//
//  AddAdressController.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/18.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "AddAdressController.h"
#import "CityPickerVeiw.h"
#import "UserModel.h"

@interface AddAdressController ()

@property (weak, nonatomic) IBOutlet UITextField *receiverNameText;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextFile;
@property (weak, nonatomic) IBOutlet UITextField *addressLab;
@property (weak, nonatomic) IBOutlet UITextView *adressDetailTextFile;

@end

@implementation AddAdressController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加地址";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(commitAction)];
    self.receiverNameText.text = AppManager.user.userName;
    self.phoneTextFile.text = AppManager.user.telephone;
    
    self.adressDetailTextFile.layer.cornerRadius = 8;
    self.adressDetailTextFile.layer.borderWidth = 1;
    self.adressDetailTextFile.layer.borderColor = [RGB(230, 230, 230) CGColor];
}

- (IBAction)selectAdressAction:(UIButton *)sender {
    CityPickerVeiw * cityView = [[CityPickerVeiw alloc] init];
    [cityView show];
    [cityView setCityBlock:^(NSString * value) {
        self.addressLab.text = value;
    }];
    return;
}

- (void) commitAction {
    if ([self.receiverNameText.text isEqualToString:@""] || [self.addressLab.text isEqualToString:@""] || [self.phoneTextFile.text isEqualToString:@""] || [self.adressDetailTextFile.text isEqualToString:@""]) {
        [MozTopAlertView showWithType:MozAlertTypeError text:@"请填写信息" parentView:nil];
        return;
    }
    NSArray *array =  [self.addressLab.text componentsSeparatedByString:@"-"];
    if (array.count == 0) {
        return;
    }
    NSDictionary *param = @{@"userId": AppManager.user.userId, @"receiverName": self.receiverNameText.text, @"provinceName": array[0], @"cityName": array[1], @"districtName": array[2], @"addressName": self.adressDetailTextFile.text, @"telephone": self.phoneTextFile.text, @"isDefault": @(0)};
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/address/list" parameters:param method:@"POST" callBack:^(id responseObject) {
        if ([responseObject[@"state"] intValue]) {
            [self.navigationController popViewControllerAnimated:true];
            return;
        }
        [MozTopAlertView showWithType:MozAlertTypeError text:@"添加地址失败" parentView:nil];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
